#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using System.Data.SqlClient;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using WaveTech.GShield.Engine.Systems;
using WaveTech.GShield.Engine.Watchers;
using WaveTech.GShield.Model;

#endregion

namespace GShield.Sentinel
{
	public class Controller
	{
		public static bool Authroized;
		private static BannedWatcher bannedWatcher;

		private static RatingsWatcher ratingsWatcher;
		private static UsageWatcher usageWatcher;

		#region Pre Flight

		public void PreFlight()
		{
			ratingsWatcher = new RatingsWatcher();
			usageWatcher = new UsageWatcher();
			bannedWatcher = new BannedWatcher();
		}

		#endregion Pre Flight

		#region Start

		public void Start()
		{
			Logging.Write("Starting Sentinel.", LogCategory.Sentinel, LogPriority.Low, LogType.System);

			if (ratingsWatcher != null)
			{
				try
				{
					ratingsWatcher.Start();
				}
				catch (SqlException)
				{
					// Most likely was due to the 
				}
			}
			else
			{
				ratingsWatcher = new RatingsWatcher();
				ratingsWatcher.Start();
			}

			Thread.Sleep(5000);

			if (usageWatcher != null)
			{
				usageWatcher.Start();
			}
			else
			{
				usageWatcher = new UsageWatcher();
				usageWatcher.Start();
			}

			Thread.Sleep(5000);

			if (bannedWatcher != null)
			{
				bannedWatcher.Start();
			}
			else
			{
				bannedWatcher = new BannedWatcher();
				bannedWatcher.Start();
			}

			InitKillSignalWatcher();
		}

		#endregion Start

		#region Stop

		public void Stop()
		{
			if (Authroized)
			{
				Logging.Write("Stoppping Sentinel.", LogCategory.Sentinel, LogPriority.Low, LogType.System);

				if (ratingsWatcher != null)
				{
					ratingsWatcher.Stop();
					ratingsWatcher = null;
				}

				if (usageWatcher != null)
				{
					usageWatcher.Stop();
					usageWatcher = null;
				}

				if (bannedWatcher != null)
				{
					bannedWatcher.Stop();
					bannedWatcher = null;
				}

				Authroized = false;
			}
		}

		#endregion Stop

		#region Init Kill Signal Watcher

		private void InitKillSignalWatcher()
		{
			Thread watcher = new Thread(KillSignalWatcher);
			watcher.Start();
		}

		#endregion Init Kill Signal Watcher

		#region Kill Signal Watcher

		private void KillSignalWatcher()
		{
			while (File.Exists(Application.StartupPath + "\\SentinelKillSig") == false)
			{
				Thread.Sleep(30000);
			}

			Logging.Write("Sentinel Kill Signal Detected.", LogCategory.Sentinel, LogPriority.High, LogType.System);

			Authroized = true;
			Stop();
			File.Delete(Application.StartupPath + "\\SentinelKillSig");
			Environment.Exit(1);
		}

		#endregion Kill Signal Watcher
	}
}